class Details{
    constructor(){
        this.details = document.querySelector('.details')
        this.pic_small = document.querySelector('.pic>img')
        this.pic_big = document.querySelector('.right>img')
        this.init()
    }
    init(){
        this.request()
    }
    //处理查询字符串
    queryStr(name){
        let arr = location.search.slice(1).split('=')
        let obj = []
        obj[arr[0]] = arr[1]
        return obj[name]
    }
    //根据id请求数据
    request(){
        let id = this.queryStr('id')
        //判断id是否存在
        if(!id){
            location.href = '../index.html'
            return
        }else{
            //存在，根据id值发送ajax请求
            pAjax({url:'../libs/goods.json'})
            .then(res=>{
                let data = JSON.parse(res)
                data = data.slice(0,100)
                this.render(id,data)
            })
        }
    }
    //通过id进行对比，把数据提取出来渲染页面
    render(id,data){
        let goods = data.find(item=>{return item.goods_id == id})
        this.pic_small.src = goods.img_small_logo
        this.pic_big.src = goods.img_big_logo
        this.details.innerHTML = goods.goods_introduce
    }
}
new Details()